
/**
 * 覆盖PfBillEventHandler.js 中的方法，此处传入后台pk，返回<pk,状态>的Map
 *
 * 此处考虑以后首先将前台主子记录删除，然后进行后台删除操作。如果后台操作成功，则调用所有主子表的
 *             clearStatus操作，如果操作失败，调用所有主子表的undo操作。
 */
function jkbxDelete()
{
	// 执行真正的删除操作之前先清空ds的状态栈
	this.callAllDsClearUndo();
	this.callAllDsClearStatus();
	var headDs = this.getMasterDs();
	if(headDs != null)
    	headDs.reqParameterMap.clear();
    if(this.beforeAction(IPfAction.DELETE) == false)
		return;
	try { 
  		// 非单表体的情况
  		if(headDs != null){
			//var deleteRows = headDs.getSelectedRows();
			var selectedIndice = headDs.getSelectedIndices();
			var deleteIndice = null; 
			if(selectedIndice != null && selectedIndice.length > 0)
			{
				deleteIndice = selectedIndice.sort(defaultIntSort);
			}
		    else{
		  		showMessageDialog(trans('ml_noselrow'));
		  		return;
		    } 
		    if(this.onBeforeBoDelete() == false)
		    	return;
		    showProgressDialog(trans('ml_wait'));
		    var count = deleteIndice.length;
		    var arr = new Array();//主ds的pk
	    	var index = getDatasetMasterKeyField(headDs);
			var djdl = null;;
			var djdlIndex = headDs.nameToIndex('djdl');
		    for(var i = count - 1 ; i >= 0; i--){
		    	var sRow = headDs.getRow(deleteIndice[i]);
		    	arr.push(sRow.getCellValue(index));
		    	djdl = sRow.getCellValue(djdlIndex);
		    }
		    var cmd = new CommonCommand("deleteByPks");
		    cmd.setParameter("delpks=" + arr.join('$') + "&djdl=" + djdl);
		    cmd.setReturnArgs([headDs,deleteIndice,this]);
		    cmd.setReturnFunc(JkbxDeleteReturnFun);
		    cmd.setCallObj(this);
		    cmd.exec();
		    
		}
	} catch(error) {
		showMessageDialog(error.name + ":" + error.message);
	}
};

/**
 * 单据删除的回调函数
 */
function JkbxDeleteReturnFun(returndoc,args,isSuccess)
{ 
	hideProgressDialog();
	if(isSuccess){
		var headDs = args[0]
		var allDelIndice = args[1];
		var oThis = args[2];
		oThis.callAllDsClearStatus();
		updateJkBxDelStatus(headDs,allDelIndice,returndoc);
		// 删除子表
		var dss = oThis.getAllSlaveDs();
		for(var i=0;i< dss.length; i++){
			//这个删除方式有问题 TODO
			dss[i].deleteRows(-1);
		} 
	    oThis.afterAction(IPfAction.DELETE);
	}
};

function updateJkBxDelStatus(headDs,allDelIndice,xmldoc){
	var maps = parseXmlToMaps(xmldoc);
	var pkIndex = getDatasetMasterKeyField(headDs);
	var delIndicesArr = new Array();
	
	for(var i=0; i<allDelIndice.length; i++){
		var row = headDs.getRow(allDelIndice[i]);
		var pk = row.getCellValue(pkIndex);
		if(maps[0].containsKey(pk)){//成功
			delIndicesArr.push(allDelIndice[i]);
		}
	}
	if(delIndicesArr.length>0)
		headDs.deleteRows(delIndicesArr);
};
